﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using yiCommerce.Core;
using yiCommerce.Models.Comments;
using yiCommerce.Models.Statuses;
using yiCommerce.Service.Statuses;

namespace yiCommerce.Controllers.api
{


   

    public class StatusApiController : ApiController
    {
        private IStatusService _statusService;
        private IWorkContext _workContext;
        private ICommentService _commentService;

        public StatusApiController(IStatusService statusService, IWorkContext workContext, ICommentService commentService)
        {
            _statusService = statusService;
            _workContext = workContext;
            _commentService = commentService;
        }


        [HttpPost]
        public GivePointResult GivePointToStatuses(GivePointRequest request)
        {
            var result = new GivePointResult();
            try
            {
                _statusService.GivePointToStatuses(request.StatusId, _workContext.CurrentCustomer.Id, 1);
                var newPoint = _statusService.GetPointOfStatus(request.StatusId);
                result.Status = ConstInfo.ResultSuccessStatus;
                result.CurrentPoint = newPoint.HasValue? newPoint.Value: 0;
            }
            catch (Exception e)
            {
                result.Message = e.Message;
                result.Status = ConstInfo.ResultFailedStatus;
            }

          
            return result;

        }

        [HttpPost]
        public HomeLineResult HomeLine(HomeLineRequest request)
        {
            var result = new HomeLineResult();
            var statuses = _statusService.HomeTimeLine(request.CustomerId, request.PageIndex, request.PageSize);
            var statusList = Mapper.StatusMapper.PrepareStatusListItemModel(statuses);
            result.StatusList = statusList;
            result.Status = "0";
            return result;

        }

        [HttpPost]
        public HomeLineProductResult HomeLineProduct(HomeLineProductRequest request)
        {
            var result = new HomeLineProductResult();
            var statuses = _statusService.HomeTimeLineProduct(request.CustomerId, request.PageIndex, request.PageSize);
            var statusList = Mapper.StatusMapper.PrepareStatusListItemModel(statuses);
            result.StatusList = statusList;
            result.Status = "0";
            return result;

        }


        [HttpPost]
        public GetPageCommentsResult GetPageComments(GetPageCommentsRequest request)
        {
            var result = new GetPageCommentsResult();
            var comments = _commentService.GetPagedComments(request.StatusId, request.PageIndex, request.PageSize);
            var commentModels = yiCommerce.Mapper.StatusMapper.ToCommentModel(comments);
            result.CommentList = commentModels;
            result.Status = "0";
            return result;
        }


    }

    public class HomeLineProductResult
    {
        public IList<StatusListItemModel> StatusList { get; set; }

        public string Status { get; set; }
    }

    public class HomeLineProductRequest
    {
        public int CustomerId { get; set; }

        public int PageSize { get; set; }

        public int PageIndex { get; set; }
    }

    public class GivePointResult
    {
        public string Status { get; set; }
        public string Message { get; set; }
        public int CurrentPoint { get; set; }
    }

    public class GivePointRequest
    {
        public int StatusId { get; set; }

    }

    public class GetPageCommentsRequest
    {
        public int StatusId{ get; set; }

        public int PageSize { get; set; }

        public int PageIndex { get; set; }
    }

    public class GetPageCommentsResult
    {
        public IList<CommentModel> CommentList{ get; set; }

        public string Status { get; set; }
    }


    public class HomeLineResult
    {
        public IList<StatusListItemModel> StatusList { get; set; }

        public string Status { get; set; }
    }

    public class HomeLineRequest
    {
        public int CustomerId { get; set; }

        public int PageSize { get; set; }

        public int PageIndex { get; set; }
    }
}
